package com.xlisteven.voice.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xlisteven.voice.pojo.Product;
import com.xlisteven.voice.service.ProductService;
import com.xlisteven.voice.utils.VoiceUtils;

@Controller
public class ProductController {

	@Autowired
	ProductService productService;

	@RequestMapping("/saveProduct")
	@ResponseBody
	public String saveProdct(Product product) {
		String orderId = UUID.randomUUID().toString().replace("-", "");
		product.setOrderId(orderId);
		String productDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
		product.setProductDate(productDate);
		int result = productService.saveProduct(product);
		if (result > 0) {
			try {
				VoiceUtils.voice(product.getProductName(), product.getProductPrice());
			} catch (IOException e) {
				e.printStackTrace();
			}
			return "success";
		}
		return "fail";
	}
}
